<template>
  <view>
    <view class="goods-list">
      <view v-for="(item,i) in goodsList" :key="i" @click="gotoDetail(item)">
        <my-goods :goods="item"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          //商品分类id
          cid: "",
          //查询关键字
          query: "",
          //当前页码
          pagenum: 1,
          //每页显示的条数
          pagesize: 10
        },
        //商品查询返回的总条数
        total: 0,
        //商品列表的数据
        goodsList: [],
        // 设置节流阀
        isloading:false
      };
    },
    onLoad(options) {
      this.queryObj.cid = options.cid || ''
      this.queryObj.query = options.query || ''
      this.getGoodsList()
    },
    onReachBottom(){
      if(this.queryObj.pagenum*this.queryObj.pagesize>=this.total) return uni.$showMsg("数据加载完毕")
      if(this.isloading) return
      this.queryObj.pagenum++
      this.getGoodsList()
    },
    onPullDownRefresh(){
      this.queryObj.pagenum=1
      this.queryObj.pagesize=10
      this.goodsList=[]
      this.total=0
      this.isloading=false
      this.getGoodsList(()=>{
        uni.stopPullDownRefresh()
      })
    },
    methods: {
      //获取商品数据列表的方法
      async getGoodsList(cd) {
        // 打开节流阀
        this.isloading=true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        // 关闭节流阀
        this.isloading=false
        //只要数据请求完毕，就立即关闭下拉刷新
        cd&&cd()
        if (res.meta.status !== 200) return uni.$showMsg()
        this.goodsList = [...this.goodsList,...res.message.goods]
        this.total = res.message.total
      },
      gotoDetail(item){
        uni.navigateTo({
          url:'../goods_detail/goods_detail?goods_id='+item.goods_id
        })
      }
    }
  }
</script>

<style lang="scss">
  .goods-list {
  }
</style>
